package com.konne.nightmare.FastPublicOpinion.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.collection.ArraySet;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.StringUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.coorchice.library.SuperTextView;
import com.konne.nightmare.FastPublicOpinion.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Create by ws.
 * on Data:2022/2/11  Time:下午 4:43.
 * description: 资源类型选择 .
 */
public class ResourceTypeDialog extends Dialog {
    private List<String> datas;
    private int type;
    private TypeAdapter mAdapter;

    public ResourceTypeDialog(@NonNull Context context) {
        super(context, R.style.ActionSheetDialogStyle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_resource_type);
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp);
        dialogWindow.setGravity(Gravity.BOTTOM);
        initData();
        initView();
    }

    private void initView() {
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
        mAdapter = new TypeAdapter(datas);
        recyclerView.setAdapter(mAdapter);
        findViewById(R.id.stv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (type == 1) {
                    type = 0;
                    mAdapter.notifyDataSetChanged();
                } else {
                    dismiss();
                }
            }
        });
        findViewById(R.id.stv_submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (type == 1) {
                    type = 0;
                    mAdapter.notifyDataSetChanged();
                } else {
                    dismiss();
                }
            }
        });
    }

    private void initData() {
        datas = new ArrayList<>();
        datas.add("文献");
        datas.add("教案");
        datas.add("录像");
        datas.add("学习资料");
        datas.add("案例实践");
        datas.add("");
    }

    class TypeAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
        private Set<String> set = new ArraySet<>();

        public TypeAdapter(@Nullable List<String> data) {
            super(R.layout.item_res_type, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, String item) {
            SuperTextView tv_title = helper.getView(R.id.tv_title);
            View sel = helper.getView(R.id.iv_sel);
            View delete = helper.getView(R.id.tv_delete);
            if (StringUtils.isEmpty(item)) {
                tv_title.setText(type == 0 ? "编辑" : "+ 添加");
                tv_title.setStrokeWidth(SizeUtils.dp2px(1));
                tv_title.setSolid(Color.WHITE);
                sel.setVisibility(View.GONE);
                delete.setVisibility(View.GONE);
            } else {
                tv_title.setText(item);
                tv_title.setStrokeWidth(SizeUtils.dp2px(0));
                tv_title.setSolid(0xffF3F4F6);
                sel.setVisibility(View.GONE);
                delete.setVisibility(View.GONE);

                if (type == 0) {
                    boolean isSel = set.contains(item);
                    tv_title.setSolid(isSel ? 0xff3396FE : 0xffF3F4F6);
                    tv_title.setTextColor(isSel ? Color.WHITE : 0xff67788A);
                    if (isSel) {
                        sel.setVisibility(View.VISIBLE);
                    }
                } else {
                    tv_title.setSolid(0xffF3F4F6);
                    tv_title.setTextColor(0xff67788A);
                    delete.setVisibility(View.VISIBLE);
                }
            }


            tv_title.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (StringUtils.isEmpty(item)) {
                        if (type == 0) {
                            type = 1;
                            notifyDataSetChanged();
                        } else {
                            showInput();
                        }
                        return;
                    }
                    if (type == 0) {
                        if (set.contains(item)) {
                            set.remove(item);
                        } else {
                            set.add(item);
                        }
                        notifyDataSetChanged();
                    }
                }
            });
            helper.setOnClickListener(R.id.tv_delete, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    datas.remove(item);
                    notifyDataSetChanged();
                }
            });

        }

        private void showInput() {
            new InputDialog(getContext()).setTitle("添加资源")
                    .setInputListener(new InputDialog.InputListener() {
                        @Override
                        public void input(String txt) {
                            if (!StringUtils.isEmpty(txt)) {
                                mData.add(mData.size()-1,txt);
                                notifyDataSetChanged();
                            }
                        }
                    }).show();
        }
    }
}
